// Copyright (C) joyinter
// All rights reserved.
// 
// Author : Lu Rong
// Date   : 2005-01-20

#include "stdafx.h"
#include ".\trenchpagectrl.h"
#include ".\trench.h"
#include <common/ui/utility.h>

// class CJoyTrenchTabPanel
//##ModelId=44C4B5E803B9
class CJoyTrenchTabPanel : public CJoyWnd
{
public:
	friend class CJoyTrenchPageCtrl;

	//##ModelId=44C4B5E803C9
	Color	m_cornerColor;
	//##ModelId=44C4B5E90000
	int		m_cornerSize;
	//##ModelId=44C4B5E90010
	Color	m_frameColor;
	//##ModelId=44C4B5E90020
	Color	m_backColor1;

	//##ModelId=44C4B5E9002E
	virtual void CM_LoadSkin(CJoyXmlElement* pSkinElem);

public:
	//##ModelId=44C4B5E9004E
	CJoyTrenchTabPanel() 
	{
		m_cornerColor.SetValue(0x00000000);
		m_frameColor.SetValue(0x00000000);
		m_backColor1.SetValue(0x00000000);
		m_cornerSize = 0;
	}
	//##ModelId=44C4B5E9005D
	virtual ~CJoyTrenchTabPanel() {}
};

//##ModelId=44C4B5E9002E
void CJoyTrenchTabPanel::CM_LoadSkin(CJoyXmlElement* pSkinElem)
{
	__super::CM_LoadSkin(pSkinElem);

	m_frameColor.SetValue(pSkinElem->ReadIntX("frame_color", m_frameColor.GetValue()));
	m_cornerColor.SetValue(pSkinElem->ReadIntX("corner_color", m_cornerColor.GetValue()));
    m_cornerSize = (int)pSkinElem->ReadIntD("corner_size", m_cornerSize);
	m_backColor1.SetValue(pSkinElem->ReadIntX("back_color1", m_backColor1.GetValue()));
}

// class CJoyTrenchTabPanel_Btn
//##ModelId=44C4B5E9006D
class CJoyTrenchTabPanel_Btn : public CJoyTrenchTabPanel
{
protected:
	//##ModelId=44C4B5E9007D
	virtual void CM_Draw(Graphics& graph);
};

//##ModelId=44C4B5E9007D
void CJoyTrenchTabPanel_Btn::CM_Draw(Graphics &graph)
{
	__super::CM_Draw(graph);

	int w = m_area.Width();
	int h = m_area.Height();
	int wc = w - m_cornerSize;

	SolidBrush backBrush(m_backColor);
	int h1 = 12;
	graph.FillRectangle(&backBrush, 0, 0, w, h1);
	JoyLinearFillRectV(graph, Rect(m_area.left, m_area.top, m_area.right, m_area.top + h1), m_backColor1, m_backColor);

	Point topRight[7] =
	{
		Point(w, 0),
		Point(w, m_cornerSize),
		Point(w - 1, m_cornerSize),
		Point(w - 1, m_cornerSize - 2),
		Point(wc + 2, 1),
		Point(wc, 1),
		Point(wc, 0),
	};

	SolidBrush cornerBrush(m_cornerColor);
	graph.FillPolygon(&cornerBrush, topRight, 7);

	Pen framePen(m_frameColor);
	SolidBrush frameBrush(m_frameColor);

	graph.FillRectangle(&frameBrush, 0, 0, wc, 1);
	graph.FillRectangle(&frameBrush, w - 1, m_cornerSize, 1, h - m_cornerSize);	

	graph.DrawLine(&framePen, w - 2, m_cornerSize, w - 2, m_cornerSize - 2);
	graph.DrawLine(&framePen, topRight[4], topRight[5]);
}

// class CJoyTrenchTabPanel_Tab
//##ModelId=44C4B5E9009C
class CJoyTrenchTabPanel_Tab : public CJoyTrenchTabPanel
{
protected:
	//##ModelId=44C4B5E900AC
	virtual void CM_Draw(Graphics& graph);
};

//##ModelId=44C4B5E900AC
void CJoyTrenchTabPanel_Tab::CM_Draw(Graphics &graph)
{
	__super::CM_Draw(graph);

	int w = m_area.Width();
	int h = m_area.Height();
	int wc = w - m_cornerSize;

	SolidBrush backBrush(m_backColor);
	int h1 = 12;
	graph.FillRectangle(&backBrush, 0, 0, w, h1);
	JoyLinearFillRectV(graph, Rect(m_area.left, m_area.top, m_area.right, m_area.top + h1), m_backColor1, m_backColor);

	Point topLeft[7] =
	{
		Point(0, 0),
		Point(0, m_cornerSize),
		Point(1, m_cornerSize),
		Point(1, m_cornerSize - 2),
		Point(m_cornerSize - 2, 1),
		Point(m_cornerSize, 1),
		Point(m_cornerSize, 0),
	};

	SolidBrush cornerBrush(m_cornerColor);
	graph.FillPolygon(&cornerBrush, topLeft, 7);

	Pen framePen(m_frameColor);
	SolidBrush frameBrush(m_frameColor);

	graph.FillRectangle(&frameBrush, m_cornerSize, 0, wc, 1);
	graph.FillRectangle(&frameBrush, 0, m_cornerSize, 1, h - m_cornerSize);
	
	graph.DrawLine(&framePen, topLeft[2], topLeft[3]);
	graph.DrawLine(&framePen, topLeft[4], topLeft[5]);
}

// class CJoyTrenchCheckBox
//##ModelId=44C4B5DA00FA
CJoyTrenchCheckBox::CJoyTrenchCheckBox()
{
	m_state = CHK_NORMAL;
	m_backColor1.SetValue(0x00000000);
	m_chkBackColor.SetValue(0x00000000);
	m_chkBackColor1.SetValue(0x00000000);
	m_hvrBackColor.SetValue(0x00000000);
	m_hvrBackColor1.SetValue(0x00000000);
	m_countColor.SetValue(0x00000000);
	m_chkCountColor.SetValue(0x00000000);
	m_chkTextColor.SetValue(0x00000000);

	m_pageCtrl = NULL;
}

//##ModelId=44C4B5DA0109
CJoyTrenchCheckBox::~CJoyTrenchCheckBox()
{
}

//##ModelId=44C4B5DA00B0
void CJoyTrenchCheckBox::CM_LoadSkin(CJoyXmlElement* pSkinElem)
{
	__super::CM_LoadSkin(pSkinElem);

	m_backColor1.SetValue(pSkinElem->ReadIntX("back_color1", m_backColor1.GetValue()));
	m_chkBackColor.SetValue(pSkinElem->ReadIntX("check_back_color", m_chkBackColor.GetValue()));
	m_chkBackColor1.SetValue(pSkinElem->ReadIntX("check_back_color1", m_chkBackColor1.GetValue()));
	m_hvrBackColor.SetValue(pSkinElem->ReadIntX("hover_back_color", m_hvrBackColor.GetValue()));
	m_hvrBackColor1.SetValue(pSkinElem->ReadIntX("hover_back_color1", m_hvrBackColor1.GetValue()));
	m_countColor.SetValue(pSkinElem->ReadIntX("count_color", m_countColor.GetValue()));
	m_chkCountColor.SetValue(pSkinElem->ReadIntX("check_count_color", m_chkCountColor.GetValue()));
	m_chkTextColor.SetValue(pSkinElem->ReadIntX("check_text_color", m_chkTextColor.GetValue()));
}

//##ModelId=44C4B5DA010B
void CJoyTrenchCheckBox::SetCheck(bool checked)
{
	__super::SetCheck(checked);

	if (checked)
	{
		m_state = CHK_CHECK;
		InvalidateCtrl();
	}
}

//##ModelId=44C4B5DA00BB
void CJoyTrenchCheckBox::CM_Mouse(MouseMessage msg, const CPoint& pt)
{
	__super::CM_Mouse(msg, pt);

	if (m_checked)
		m_state = CHK_CHECK;
	else
		m_state = CHK_NORMAL;

	InvalidateCtrl(); 
}

//##ModelId=44C4B5DA00CC
void CJoyTrenchCheckBox::CM_MouseHover()
{
	__super::CM_MouseHover();

//	if (m_state != CHK_CHECK)
//	{
//		m_state = CHK_HOVER;
//		InvalidateCtrl();
//	}
}

//##ModelId=44C4B5DA00DB
void CJoyTrenchCheckBox::CM_MouseLeave()
{
/*	__super::CM_MouseLeave();

	if (m_state != CHK_CHECK)
	{
		m_state = CHK_NORMAL;
		InvalidateCtrl();
	}*/
}

//##ModelId=44C4B5DA00EA
void CJoyTrenchCheckBox::CM_Draw(Graphics& graph)
{
	__super::CM_Draw(graph);

	int w = m_area.Width();
	int h = m_area.Height();

	int h1 = 5;

	Color backColor(m_backColor);
	Color backColor1(m_backColor1);
	Color backColor2(0xFF525252);

	Color txtColor(m_textColor);
	Color countColor(m_countColor);

	//bool b=m_checked;

	if (m_checked)
	{
		backColor.SetValue(m_chkBackColor.GetValue());
		backColor1.SetValue(m_chkBackColor1.GetValue());
		backColor2.SetValue(m_chkBackColor.GetValue());

		txtColor.SetValue(m_chkTextColor.GetValue());
		countColor.SetValue(m_chkCountColor.GetValue());
	}
	else
	{
		if (m_btnDown)
		{
			backColor.SetValue(m_hvrBackColor.GetValue());
			backColor1.SetValue(m_hvrBackColor1.GetValue());
			h1 = 4;
		}
		else
		{
			backColor.SetValue(m_backColor.GetValue());
			backColor1.SetValue(m_backColor1.GetValue());
		}
	}

/*	switch (m_state)
	{
	case CHK_HOVER:
		backColor.SetValue(m_hvrBackColor.GetValue());
		backColor1.SetValue(m_hvrBackColor1.GetValue());
		h1 = 4;
		break;
	case CHK_CHECK:
		backColor.SetValue(m_chkBackColor.GetValue());
		backColor1.SetValue(m_chkBackColor1.GetValue());
		break;
	case CHK_NORMAL:
		backColor.SetValue(m_backColor.GetValue());
		backColor1.SetValue(m_backColor1.GetValue());
		break;
	default:
		break;
	}*/

	SolidBrush backBrush(backColor);
	SolidBrush backBrush1(backColor1);
	graph.FillRectangle(&backBrush, 0, 0, w, h1);
	if (m_state == CHK_HOVER)
		graph.FillRectangle(&backBrush1, 0, h1, w, h - h1);
	else
		JoyLinearFillRectV(graph, Rect(0, h1, w, h - h1), backColor1, backColor2); 

/*	Color interColor[] = 
    {
        Color(backColor.GetValue()),
		Color(backColor1.GetValue()),
        Color(backColor.GetValue()),            
    };
    REAL interPos[] = 
    {
        0.0f, (REAL)h1 / h, 1.0f,
    };
    JoyLinearFillRectV(graph, Rect(0, 0, w, h), interColor, interPos, 3);*/

	// draw text
    StringFormat sf;
	sf.SetLineAlignment(StringAlignmentCenter);
	sf.SetTrimming(StringTrimmingEllipsisCharacter);
	sf.SetFormatFlags(StringFormatFlagsNoWrap);
	FontFamily fam(m_textFace);
	Font font(&fam, (REAL)m_textSize, FontStyleRegular, UnitPixel);

	CStringW strName;
	CStringW strCount;
	if (m_pageCtrl != NULL)
	{
		POSITION pos = m_pageCtrl->GetItemPos(m_name);
		if (pos != NULL)
		{
			CJoyPageCtrl::CPageItem &item = m_pageCtrl->m_items.GetAt(pos);
			CJoyTrench *trench = static_cast<CJoyTrench *>(item.m_page);
			strName = trench->m_trenchName;
			strCount.Format(L"(%d/%d)", trench->m_playingCount, trench->m_maxPlayerCount);
		}
	}
	SolidBrush txBrush(txtColor);
	SolidBrush cntBrush(countColor);
//	REAL cw = 29.0f;
//	REAL bw = 3.0f;
	REAL cw = 44.0f;
	graph.DrawString(strName, strName.GetLength(), &font, 
		RectF((REAL)1, (REAL)h1, (REAL)w - cw - 1, (REAL)h - h1), 
		&sf, &txBrush);
	sf.SetTrimming(StringTrimmingNone);
	graph.DrawString(strCount, strCount.GetLength(), &font, 
		RectF((REAL)w - cw, (REAL)h1, (REAL)cw, (REAL)h - h1), 
		&sf, &cntBrush);/**/

//	CStringW bracket_l("()");
//	graph.DrawString(bracket_l, bracket_l.GetLength(), &font, 
//		RectF((REAL)w - cw - 2 * bw + 1, (REAL)h1, (REAL)bw, (REAL)h - h1), 
//		&sf, &txBrush);

//	graph.DrawString(strCount, strCount.GetLength(), &font, 
//		RectF((REAL)w - cw - bw, (REAL)h1, (REAL)cw, (REAL)h - h1), 
//		&sf, &cntBrush);

//	CStringW bracket_r(")");
//	graph.DrawString(bracket_r, bracket_r.GetLength(), &font, 
//		RectF((REAL)w - bw, (REAL)h1, (REAL)bw, (REAL)h - h1), 
//		&sf, &txBrush);

	
	// draw corner
	if (m_area.left == 2)
	{
		Point topLeft[3] =
		{
			Point(0, 0),
			Point(0, 2),
			Point(2, 0),
		};
		SolidBrush corner(0xFF000000);
		graph.FillPolygon(&corner, topLeft, 3);
	}
	if (m_area.left <= 0)
	{
		int s = 0;
		CJoyTrenchTabPanel_Btn *pt = static_cast<CJoyTrenchTabPanel_Btn *>(this->m_parent);
		SolidBrush tabSetFrame(pt->m_frameColor);
		SolidBrush tabSetBack(0xFF000000);	
		s = - m_area.left;
		graph.FillRectangle(&tabSetBack, s, 0, 2, h);
		graph.FillRectangle(&tabSetFrame, s, 2, 1, h - 2);
		graph.FillRectangle(&tabSetFrame, s + 1, 0, 1, 2);

		Point topLeft[3] =
		{
			Point(s + 2, 0),
			Point(s + 2, 2),
			Point(s + 4, 0),
		};
		graph.FillPolygon(&tabSetBack, topLeft, 3);
	}

//	RoundRect(graph.GetHDC(), 4, 4, 40, 18, 2, 2);
}

// class CJoyTrenchPageContainer

IMPLEMENT_DYNAMIC(CJoyTrenchPageContainer, CJoyPageContainer)

//##ModelId=44C4B5DA016A
CJoyTrenchPageContainer::CJoyTrenchPageContainer()
{
//	m_pageCtrl	= pageCtrl;
	m_frameColor.SetValue(0x00000000);
	m_cornerSize = 0;
}

//##ModelId=44C4B5DA0177
CJoyTrenchPageContainer::~CJoyTrenchPageContainer()
{
}

BEGIN_MESSAGE_MAP(CJoyTrenchPageContainer, CJoyPageContainer)
END_MESSAGE_MAP()

//##ModelId=44C4B5DA015C
void CJoyTrenchPageContainer::CM_LoadSkin(CJoyXmlElement* pSkinElem)
{
	__super::CM_LoadSkin(pSkinElem);

    m_frameColor.SetValue(pSkinElem->ReadIntX("frame_color", m_frameColor.GetValue()));
    m_cornerSize = (int)pSkinElem->ReadIntD("corner_size", m_cornerSize);
}

//##ModelId=44C4B5DA0167
void CJoyTrenchPageContainer::CM_Draw(Graphics& graph)
{
	__super::CM_Draw(graph);

	int w = m_area.Width();
    int h = m_area.Height();

    Pen framePen(m_frameColor);
	graph.DrawLine(&framePen, 0, 0, 0, h);
	graph.DrawLine(&framePen, w - 1, 0, w - 1, h);
}

// class CJoyTrenchTabSet

IMPLEMENT_DYNAMIC(CJoyTrenchPageTabSet, CJoyPageTabSet)

//##ModelId=44C4B5DA01F6
CJoyTrenchPageTabSet::CJoyTrenchPageTabSet()
{
//	m_pageCtrl = pageCtrl;
	m_cornerColor.SetValue(0x00000000);
	m_cornerSize = 0;
	m_frameColor.SetValue(0x00000000);
	m_backColor1.SetValue(0x00000000);

	m_step = 0;

	m_btnLeft = NULL;
	m_btnRight = NULL;

	m_paneOfBtn = NULL;
	m_paneOfTab = NULL;
}

//##ModelId=44C4B5DA01F7
CJoyTrenchPageTabSet::~CJoyTrenchPageTabSet()
{
}

BEGIN_MESSAGE_MAP(CJoyTrenchPageTabSet, CJoyPageTabSet)
	ON_WM_CREATE()
END_MESSAGE_MAP()

//##ModelId=44C4B5DA01E7
void CJoyTrenchPageTabSet::CM_LoadSkin(CJoyXmlElement* pSkinElem)
{
	__super::CM_LoadSkin(pSkinElem);

	m_step = (int)pSkinElem->ReadIntD("step", m_step);
}

//##ModelId=44C4B5DA01DE
void CJoyTrenchPageTabSet::CM_LeftClick(CJoyButton*)
{
	CJoyTrenchPageCtrl * page = static_cast<CJoyTrenchPageCtrl *>(m_pageCtrl);
	if (page->GetTabSetWidth() > m_area.Width())
	{
		page->m_leftVisible += m_step + page->m_tabSpace;
		page->BuildTabArea();
	}	
}

//##ModelId=44C4B5DA01E5
void CJoyTrenchPageTabSet::CM_RightClick(CJoyButton*)
{
	CJoyTrenchPageCtrl * page = static_cast<CJoyTrenchPageCtrl *>(m_pageCtrl);
	if (page->GetTabSetWidth() > m_area.Width())
	{
		page->m_leftVisible -= m_step + page->m_tabSpace;
		page->BuildTabArea();
	}
}

//##ModelId=44C4B5DA0205
int CJoyTrenchPageTabSet::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
	if (CJoyPageTabSet::OnCreate(lpCreateStruct) == -1)
		return -1;

	m_paneOfBtn = new CJoyTrenchTabPanel_Btn();
	m_paneOfBtn->m_name = "pane_btn";
	m_paneOfBtn->m_skinAlign = true;
	m_paneOfBtn->m_skinArea = true;
	m_paneOfBtn->Create(this, CRect(0, 0, 10, 10));

	m_paneOfTab = new CJoyTrenchTabPanel_Tab();
	m_paneOfTab->m_name = "pane_tab";
	m_paneOfTab->m_skinAlign = true;
	m_paneOfTab->m_skinArea = true;
	m_paneOfTab->Create(this, CRect(0, 0, 10, 10));

	m_btnLeft = new CJoyButton();
    m_btnLeft->m_name = "left";
	m_btnLeft->m_skinArea = true;
    m_btnLeft->SetClickEvent(this, static_cast<CJoyButton::PClickEvent>(&CJoyTrenchPageTabSet::CM_LeftClick));
    m_btnLeft->Create(m_paneOfBtn, CRect(0, 0, 10, 10));

	m_btnRight = new CJoyButton();
    m_btnRight->m_name = "right";
	m_btnRight->m_skinArea = true;
    m_btnRight->SetClickEvent(this, static_cast<CJoyButton::PClickEvent>(&CJoyTrenchPageTabSet::CM_RightClick));
    m_btnRight->Create(m_paneOfBtn, CRect(0, 0, 10, 10));

	return 0;
}

// class CJoyTrenchPageCtrl

IMPLEMENT_DYNAMIC(CJoyTrenchPageCtrl, CJoyPageCtrl)

//##ModelId=44C4B5DA0275
CJoyTrenchPageCtrl::CJoyTrenchPageCtrl(void)
{
	m_name		= "trench_page_ctrl";
    m_align		= AT_Client;
	
	m_leftVisible = 0;
}

//##ModelId=44C4B5DA0281
CJoyTrenchPageCtrl::~CJoyTrenchPageCtrl(void)
{
}

//##ModelId=44C4B5DA0251
CJoyPageContainer * CJoyTrenchPageCtrl::CreateContainer()
{
	return new CJoyTrenchPageContainer();
}

//##ModelId=44C4B5DA0253
CJoyPageTabSet * CJoyTrenchPageCtrl::CreateTabSet()
{
	return new CJoyTrenchPageTabSet();
}

BEGIN_MESSAGE_MAP(CJoyTrenchPageCtrl, CJoyPageCtrl)
	ON_WM_CREATE()
END_MESSAGE_MAP()

//##ModelId=44C4B5DA0284
int CJoyTrenchPageCtrl::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
	if (CJoyPageCtrl::OnCreate(lpCreateStruct) == -1)
		return -1;

	return 0;
}

//##ModelId=44C4B5DA0255
void CJoyTrenchPageCtrl::LoadSkin(CJoyXmlElement* pSkinElem)
{
	__super::LoadSkin(pSkinElem);	

#if 1
	CJoyXmlElement *pSkinTab = pSkinElem->FindElement("tab_set");
	m_skinTab = pSkinTab->GetElement("pane_tab");
	m_skinPage = pSkinElem->FindElement("page_container");
#else
	POSITION pos;
	CJoyXmlElement *p= pSkinElem->FindElement("tab_set");
	CJoyXmlElement *m_skinelem = p->GetElement("pane_tab");
	CJoyXmlElement *pp = pSkinElem->FindElement("page_container");

	CJoyTrench *ptrench01 = new CJoyTrench();
	ptrench01->m_room = m_room;
	ptrench01->m_pageCtrl = this;
	ptrench01->m_trenchName = "Joyinter01";
	ptrench01->m_gameName = "Joyinter01";
	ptrench01->m_csMapName = "!17_iceworld";
	ptrench01->m_backColor.SetValue(0xFFCCCCCC);
	AddPage("trench", ptrench01);
	pos = GetItemPos("trench");

	if (pos != NULL)
	{
		CPageItem &item = m_items.GetAt(pos);
		CJoyCheckBox *p = item.m_tab;
		p->LoadSkin(m_skinelem->GetElement("trench_tab_def"));
		item.m_page->LoadSkin(pp->GetElement("trench_def"));
	}

	CJoyTrench *ptrench = new CJoyTrench();
	ptrench->m_room = m_room;
	ptrench->m_pageCtrl = this;
	ptrench->m_trenchName = "Joyinter02";
	ptrench->m_gameName = "Joyinter02";
	ptrench->m_csMapName = "de_dust2";
	ptrench->m_backColor.SetValue(0xFF00FF00);
	AddPage("trench01", ptrench);
	pos = GetItemPos("trench01");

	if (pos != NULL)
	{
		CPageItem &item = m_items.GetAt(pos);
		CJoyCheckBox *p = item.m_tab;
		p->LoadSkin(m_skinelem->GetElement("trench_tab_def"));
		item.m_page->LoadSkin(pp->GetElement("trench_def"));
	}

	CJoyTrench *ptrench02 = new CJoyTrench();
	ptrench02->m_room = m_room;
	ptrench02->m_pageCtrl = this;
	ptrench02->m_trenchName = "Joyinter03";
	ptrench02->m_gameName = "Joyinter03";
	ptrench02->m_csMapName = "ab_blidestrike";
	ptrench02->m_backColor.SetValue(0xFF666666);
	AddPage("trench02", ptrench02);
	pos = GetItemPos("trench02");

	if (pos != NULL)
	{
		CPageItem &item = m_items.GetAt(pos);
		CJoyCheckBox *p = item.m_tab;
		p->LoadSkin(m_skinelem->GetElement("trench_tab_def"));
		item.m_page->LoadSkin(pp->GetElement("trench_def"));
	}

	CJoyTrench *ptrench03 = new CJoyTrench();
	ptrench03->m_room = m_room;
	ptrench03->m_pageCtrl = this;
	ptrench03->m_trenchName = "Joyinter04";
	ptrench03->m_gameName = "Joyinter04";
	ptrench03->m_csMapName = "de_dust2";
	ptrench03->m_backColor.SetValue(0xFF666666);
	AddPage("trench03", ptrench03);
	pos = GetItemPos("trench03");

	if (pos != NULL)
	{
		CPageItem &item = m_items.GetAt(pos);
		CJoyCheckBox *p = item.m_tab;
		p->LoadSkin(m_skinelem->GetElement("trench_tab_def"));
		item.m_page->LoadSkin(pp->GetElement("trench_def"));
	}

	CJoyTrench *ptrench04 = new CJoyTrench();
	ptrench04->m_room = m_room;
	ptrench04->m_pageCtrl = this;
	ptrench04->m_trenchName = "Joyinter05";
	ptrench04->m_gameName = "Joyinter05";
	ptrench04->m_csMapName = "de_dust";
	ptrench04->m_backColor.SetValue(0xFF666666);
	AddPage("trench04", ptrench04);
	pos = GetItemPos("trench04");

	if (pos != NULL)
	{
		CPageItem &item = m_items.GetAt(pos);
		CJoyCheckBox *p = item.m_tab;
		p->LoadSkin(m_skinelem->GetElement("trench_tab_def"));
		item.m_page->LoadSkin(pp->GetElement("trench_def"));
	}

	CJoyTrench *ptrench05 = new CJoyTrench();
	ptrench05->m_room = m_room;
	ptrench05->m_pageCtrl = this;
	ptrench05->m_trenchName = "Joyinter06";
	ptrench05->m_gameName = "Joyinter06";
	ptrench05->m_csMapName = "de_office";
	ptrench05->m_backColor.SetValue(0xFF666666);
	AddPage("trench05", ptrench05);
	pos = GetItemPos("trench05");

	if (pos != NULL)
	{
		CPageItem &item = m_items.GetAt(pos);
		CJoyCheckBox *p = item.m_tab;
		p->LoadSkin(m_skinelem->GetElement("trench_tab_def"));
		item.m_page->LoadSkin(pp->GetElement("trench_def"));
	}
#endif
}

//##ModelId=44C4B5DA0263
void CJoyTrenchPageCtrl::BuildTabArea()
{
    BuildTabAreaTop();

    if(m_container != NULL)
    {
        ((CJoyCtrl*)m_container)->InvalidateCtrl();
    }
}

//##ModelId=44C4B5DA0264
void CJoyTrenchPageCtrl::BuildTabAreaTop()
{
	CJoyTrenchPageTabSet * tabset = static_cast<CJoyTrenchPageTabSet *>(m_tabSet);

	int hw = GetTabSetWidth();
	CRect area = tabset->m_paneOfTab->GetArea();
	int vw = area.Width();

	if (m_leftVisible + hw < vw)
		m_leftVisible = vw - hw;

	if (m_leftVisible > 0)
		m_leftVisible = 0;

	int tabStart = m_leftVisible + m_tabStart;

    POSITION pos = m_items.GetHeadPosition();
    while (pos)
    {
        CPageItem &item = m_items.GetNext(pos);
        CJoyCheckBox *tab = item.m_tab;
        int w = tab->GetArea().Width();
        int h = tab->GetArea().Height();
		int top = area.Height() - h;
		int left = tabStart;
		tabStart += w;
		tabStart += m_tabSpace;
		tab->SetArea(CRect(left, top, left + w, top + h));
	}
}

//##ModelId=44C4B5DA0271
void CJoyTrenchPageCtrl::AddPage(const CString& name, CJoyWnd* page)
{
	// must not be created.
    ASSERT(page->m_hWnd == NULL);

	CJoyTrenchPageTabSet * tabset = static_cast<CJoyTrenchPageTabSet *>(m_tabSet);

    CPageItem item;
    item.m_name = name;
    item.m_page = page;
    item.m_tab  = new CJoyTrenchCheckBox();
	static_cast<CJoyTrenchCheckBox *>(item.m_tab)->m_pageCtrl = this;
    item.m_tab->m_name = name;
	item.m_tab->Create(tabset->m_paneOfTab, CRect(0, 0, 10, 10));
//	item.m_tab->Create(m_tabSet, CRect(0, 0, 10, 10));
    item.m_tab->SetCheckEvent(this, static_cast<CJoyCheckBox::PCheckEvent>(&CJoyPageCtrl::OnSelTab));

    if(page->m_name.IsEmpty())
    {
        page->m_name = name;
    }
    page->Create(m_container, CRect(0, 0, 10, 10));
    page->SetVisible(false);

    m_items.AddTail(item);
    m_tabGroup.Add(item.m_tab);

    BuildTabArea();
}

//##ModelId=44C4B5DA0265
int CJoyTrenchPageCtrl::GetTabSetWidth()
{
	int len = 0;
	POSITION pos = m_items.GetHeadPosition();
    while(pos)
    {
        CPageItem& item = m_items.GetNext(pos);
        CJoyCheckBox* tab = item.m_tab;
        int w = tab->GetArea().Width();
		len += w + m_tabSpace;        
    }
	return len;
}

//##ModelId=44C4B5DA0274
void CJoyTrenchPageCtrl::RemoveAll()
{
	POSITION pos = m_items.GetHeadPosition();
	while (pos != NULL)
	{
		POSITION tempPos = pos;
		CPageItem &item = m_items.GetNext(pos);

		item.m_tab->Destroy();
        delete item.m_tab;
		
		item.m_page->Destroy();
		delete item.m_page;

        m_items.RemoveAt(tempPos);

    //    BuildTabArea();
	}
}
/*
CJoyWnd* CJoyTrenchPageCtrl::RemovePage(const CString& name)
{
	POSITION pos = GetItemPos(name);
    if(pos != NULL)
    {
        CPageItem& item = m_items.GetAt(pos);

        CJoyWnd* page = item.m_page;

		// added by lr
		CJoyTrenchPageTabSet * tabset = static_cast<CJoyTrenchPageTabSet *>(m_tabSet);
		tabset->m_paneOfTab->RemoveChild(item.m_tab);
		CJoyTrenchPageContainer *container = static_cast<CJoyTrenchPageContainer *>(m_container);
		container->RemoveChild(item.m_page);
		item.m_page->Destroy();
		delete item.m_page;

        item.m_tab->Destroy();
        delete item.m_tab;
        m_items.RemoveAt(pos);

        BuildTabArea();

        //return page;
    }
    return NULL;
}

POSITION CJoyTrenchPageCtrl::GetItemPos(const CString& name)
{
	POSITION pos = m_items.GetHeadPosition();
    while(pos)
    {
        POSITION tempPos = pos;
        CPageItem& item = m_items.GetNext(pos);
        if(item.m_name == name)
        {
            return tempPos;
        }
    }
    return NULL;
}*/
